package com.zhai.netty.book.chapter04.linebase;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class TimeServerHandler044 extends ChannelHandlerAdapter{
	
	private int counter;
 	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		String body = (String) msg;
		++counter;
		System.out.println("msg body : " + body +" current counter is " + counter);
		String resp = ("服务器返回客户端msg current counter is"+counter + System.getProperty("line.separator"));
		ByteBuf buf = Unpooled.copiedBuffer(resp.getBytes());
		ctx.writeAndFlush(buf);
	}
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
//		String resp = ("服务器返回客户端msg current counter is"+counter + System.getProperty("line.separator"));
//		ByteBuf buf = Unpooled.copiedBuffer(resp.getBytes());
//		ctx.writeAndFlush(buf);
	}
	
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.err.println("服务器端捕获异常");
		cause.printStackTrace();
		ctx.close();
	}
}
 